package com.hyphenate.easeui.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.method.Touch;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.rockerhieu.emojicon.EmojiconTextView1;
import com.rockerhieu.emojicon.emoji.SerializeManager;

import java.util.List;

public class TextViewFixTouchConsume extends EmojiconTextView1 {
boolean dontConsumeNonUrlClicks = false;
boolean linkHit;
    boolean ok;
    Context context;

    public TextViewFixTouchConsume(Context context) {
    super(context);
    this.context=context;

    }

public TextViewFixTouchConsume(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;

}

public TextViewFixTouchConsume(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context=context;

}
    private long stara;
    private long last;
@Override
public boolean onTouchEvent(MotionEvent event) {
//    linkHit = false;
    boolean res = super.onTouchEvent(event);
    int action = event.getAction();
    if(action == MotionEvent.ACTION_DOWN){
        stara=System.currentTimeMillis();
        ok=false;
    }
    if(action !=MotionEvent.ACTION_DOWN){
        last=System.currentTimeMillis();
        if(last-stara>1000){
            ok=true;
            linkHit=false;
        }else{
            ok=false;
        }
    }
    Log.e("ac","action "+action +" ---ok "+ok + "linkHit--"+linkHit);
    if (linkHit&&!ok) {
        return true;
    }else {
        return false;
    }

}

    public static class LocalLinkMovementMethod extends LinkMovementMethod {
    static LocalLinkMovementMethod sInstance;

    public static LocalLinkMovementMethod getInstance() {
        if (sInstance == null)
            sInstance = new LocalLinkMovementMethod();

        return sInstance;
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
        int action = event.getAction();
        if(action == MotionEvent.ACTION_UP){
            if (widget instanceof TextViewFixTouchConsume){
                ((TextViewFixTouchConsume) widget).last = System.currentTimeMillis();
            }
        }
        if (action == MotionEvent.ACTION_UP ||
                action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    Log.e("ac1","action== "+action );
//                    ((TextViewFixTouchConsume) widget).last=System.currentTimeMillis();
//                    if( ((TextViewFixTouchConsume) widget).last- ((TextViewFixTouchConsume) widget).stara>1000){
//                        ((TextViewFixTouchConsume) widget).ok=true;
//                    }else{
                        link[0].onClick(widget);
//                    }
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer,
                            buffer.getSpanStart(link[0]),
                            buffer.getSpanEnd(link[0]));
                    Log.e("ac1","action=___ "+action );
                }

                if (widget instanceof TextViewFixTouchConsume){
                    ((TextViewFixTouchConsume) widget).linkHit = true;
                }
                return true;
            } else {
                Selection.removeSelection(buffer);
                Touch.onTouchEvent(widget, buffer, event);
                return false;
            }
        }
        return Touch.onTouchEvent(widget, buffer, event);
    }
}
    @Override
    public boolean hasFocusable() {
        return false;
    }
    public void setText1(String html,Intent intent) {
//		CharSequence sequence = Html.fromHtml(html);
        SpannableStringBuilder strBuilder = new SpannableStringBuilder(html);
        String content = html;
        if (content.equals("")) {
        } else {
            List<String> tempL = SerializeManager.getUrlsInContent(content);
            if(tempL != null){
                for(String temp : tempL){
                    int index = content.indexOf(temp);
                    strBuilder.setSpan(new URLClickSpan(temp,intent), index, index+temp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }

            setText(strBuilder);


        }
    }
    private class URLClickSpan extends URLSpan {
        String text;
        Intent intent;
        public URLClickSpan(String text,Intent intent) {
            super(text);
            this.text = text;
            this.intent=intent;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#f55857"));
            ds.setUnderlineText(true); //ȥ���»���
        }

        @Override
        public void onClick(View widget) {
            //���������ʱ����
            if(null!=intent){
                Log.e("aaaa","--------");
                intent.putExtra("url", text);
                context.startActivity(intent);
            }
        }
    }
}